package ebiz.buddhist.texts.network;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.app.Activity;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;


public final class NetworkUtil {
	
	public static Activity act;
	public String noData = "Activity Test";

   private NetworkUtil() {
   }

   public static String getIpAddress() {
      try {
         for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
               InetAddress inetAddress = enumIpAddr.nextElement();
               if (!inetAddress.isLoopbackAddress()) {
                  return inetAddress.getHostAddress().toString();
               }
            }
         }
      } catch (SocketException e) {
        // Log.e(Constants.LOG_TAG, e.getMessage(), e);
      }
      return null;
   }

   public static boolean connectionPresent(final ConnectivityManager cMgr) {
      if (cMgr != null) {
         NetworkInfo netInfo = cMgr.getActiveNetworkInfo();
         if ((netInfo != null) && (netInfo.getState() != null)) {
            return netInfo.getState().equals(State.CONNECTED);
         } else {
            return false;
         }
      }
      return false;
   }
  /* 
   public static void urlTest()
   {
	   int fileSize;
		try{
	 	String urlstr = "https://sites.google.com/site/sekisuri/downcheck.mp3";
	    URL url = new URL(urlstr);
	    if (act != null)
			((BuddhistApp) act.getApplication()).startLoading(act);
	    
       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();   
       urlConnection.setRequestMethod("GET");   
       urlConnection.setDoOutput(true);
       fileSize = urlConnection.getContentLength(); 
       urlConnection.disconnect();
       if (act != null)
			((BuddhistApp) act.getApplication()).endLoading();
       
       if (fileSize > 0)
       {
   // 	   return true;
       }
       else
       {
   // 	   return false;
       }
		} catch (MalformedURLException e) {   
         e.printStackTrace();   
        } catch (IOException e) {   
         e.printStackTrace();  
        }
	   //return false;
   }*/
}
